package com.serkol.ams.controller.api;

import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.serkol.ams.entity.OrderDetail;
import com.serkol.ams.entity.OrderMaster;
import com.serkol.ams.service.CloudInventoryService;
import com.serkol.ams.service.OrderDetailService;
import com.serkol.ams.service.OrderMasterService;
import com.serkol.ams.service.PrintersService;
import com.serkol.exception.AuthException;
import com.serkol.exception.SucsessException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/payment/notify")
public class PaymentNotifyApiController {
    @Autowired
    private OrderMasterService masterService;
    @Autowired
    private OrderDetailService detailService;
    @Autowired
    private CloudInventoryService cloudInventoryService;
    @Autowired
    private WxPayService wxPayService;
    @Autowired
    private PrintersService printersService;
    @PostMapping("order")
    public String order(@RequestBody String xmlData) throws WxPayException, AuthException, SucsessException {
        System.out.println("-------------------------HHHHH-------------------------------");
        System.out.println(xmlData);
        WxPayOrderNotifyResult wxPayOrderNotifyResult = wxPayService.parseOrderNotifyResult(xmlData);
        System.out.println(wxPayOrderNotifyResult);
        String resultCode = wxPayOrderNotifyResult.getResultCode();
        OrderMaster order = masterService.getBySn(wxPayOrderNotifyResult.getOutTradeNo());
        if(order.getIsPay()!=1){
        if (StringUtils.equals(resultCode, "SUCCESS")) {
            if (order != null) {
                List<OrderDetail> byOrderDetailList = detailService.findByOrderList(order.getId());
                boolean save = false;
                if (order.getIsAgent() == 1) {
                    save = masterService.saveAgentOrderDetails(order, byOrderDetailList, order.getAgentId());
                } else {
                    if(order.getIsUpgrade()==1) {
                        save = masterService.saveAgentOrderCreateDetails(order, byOrderDetailList);
                    }else{
                        OrderDetail orderDetail = byOrderDetailList.get(0);
                        if(orderDetail.getGoodsId()==35 || orderDetail.getGoodsId()==34){
                           save=true;
                        }else {
                            save = masterService.saveOrderCreate(order, byOrderDetailList);
                        }
                    }
                }
                if (save) {
                    order.setIsPay(1);
                    order.setPaymentDate(new Date());
                    order.setPartySn(wxPayOrderNotifyResult.getTransactionId());
                    order.setPaymentStatus(2);
                    order.setStatus(2);
                    boolean b = masterService.updateById(order);
                    if (b) {
                        if (order.getCloudType() == 1) {
                            boolean cb = cloudInventoryService.saveOrder(byOrderDetailList);
                        }
                        printersService.sendPrinter(order.getId());
                    }


                }
                return save ? "SUCCESS" : "FAIL";
            } else {
                return "FAIL";
            }
        } else {

            order.setPaymentDate(new Date());
            order.setPartySn(wxPayOrderNotifyResult.getTransactionId());
            order.setPaymentStatus(3);
            order.setStatus(3);
            masterService.updateById(order);
            return "FAIL";
        }
        }else {
            return  "SUCCESS";
        }

    }
}
